"""
查找替换 date_format 类型转换的
"""

import logging
import re


def change_date_format_hyphen_day(m):
    logging.info("change_date_format_hyphen_day >>> ")
    new_date_format = "TO_CHAR({}, 'yyyy-MM-dd')".format(str(m.group(2)).strip())
    return new_date_format


def change_date_format_day(m):
    new_date_format = "TO_CHAR({}, 'yyyyMMdd')".format(str(m.group(2)).strip())
    return new_date_format


def change_df_current_day_hyphen(m):
    logging.info(" change_df_current_day_hyphen ")
    new_date_format = "TO_CHAR(now(), 'yyyy-MM-dd')"
    return new_date_format


def change_df_current_day(m):
    logging.info(" change_df_current_day ")
    new_date_format = "TO_CHAR(now(), 'yyyy-MM-dd')"
    return new_date_format


def replace_simple_date_format(document_content):
    logging.info(" replace_simple_date_format ")
    df_patten = re.compile(r"(DATE_FORMAT|date_format)\(([^\(\)]*)\,\'\%Y\-\%m\-\%d\'\)")
    result = df_patten.sub(change_date_format_hyphen_day, document_content)
    document_content = result

    df_patten = re.compile(r"(DATE_FORMAT|date_format)\((.*)\,\'\%Y\%m\%d\'\)")
    result = df_patten.sub(change_date_format_day, document_content)
    document_content = result

    # date_format(now(), '%Y-%m-%d')
    df_patten = re.compile(r"(DATE_FORMAT|date_format)\((NOW|now)\(\)\,\'\%Y\-\%m\-\%d\'\)")
    result = df_patten.sub(change_df_current_day_hyphen, document_content)
    document_content = result

    df_patten = re.compile(r"(DATE_FORMAT|date_format)\((NOW|now)\(\)\,\'\%Y\%m\%d\'\)")
    result = df_patten.sub(change_df_current_day, document_content)
    document_content = result

    return document_content

# replace_simple_date_format
